package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.home.objects.clsPrograma;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class Documentos extends Activity {
	Intent intent;
	private String PUT_EVENTO;
	private Dictionary dic;
	private String UrlPrueba = "";
	private Dialog progressDialog;
	private ArrayList<Dictionary> Actividades;
	private String[] arrayDoc, arrayDocUrl;
	private ListView lista;

	public void voidFinish(View V) {
		finish();
	}

	public void abrirMenuLateral(View V) {
		intent = new Intent(Documentos.this, MenuLateral.class);
		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_documentos);

		dic = clsPrograma.getDctionary();

		UrlPrueba = getString(R.string.QRY_URL)
				+ Uri.encode("select * from ponencias_documentos where nen_idr="
						+ dic.get("nen_idr").toString());

		// Log.i("DIC", ""+dic.get("nen_idr").toString());

		new LoadHtml().execute("");

	}

	public class LoadHtml extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			progressDialog = ProgressDialog.show(Documentos.this, "",
					"Cargando...", true, true, new OnCancelListener() {

						@Override
						public void onCancel(DialogInterface arg0) {
							finish();
						}
					});

		}

		@Override
		protected String doInBackground(String... params) {

			// String qry_url =
			// "http://200.23.150.14:8080/Cintermex/DataServlet?Query="
			// +
			// Uri.encode("select a.* from eventos_banners a inner join banners_catalogo "
			// +
			// "b on a.ban_idr = b.ban_idr where a.eve_idr ="+PUT_EVENTO+"and "
			// +
			// "b.ban_nombre = 'BANNER HEADER MAIL' ORDER BY b.ban_nombre");
			// Log.i("URL", ""+qry_url);
			//
			// saxXML saxparser = new saxXML(qry_url);

			saxXML saxparser = new saxXML(UrlPrueba);
			// Log.i("UrlPrueba", ""+UrlPrueba);

			Actividades = saxparser.parse();

			Log.i("array size", "" + Actividades.size());
			arrayDoc = new String[Actividades.size()];
			arrayDocUrl = new String[Actividades.size()];

			if (Actividades.size() > 0) {
				for (int i = 0; i < Actividades.size(); i++) {
					arrayDoc[i] = "" + Actividades.get(i).get("doc_nombre");
					arrayDocUrl[i] = "" + Actividades.get(i).get("doc_url");

					Log.i("info", "" + Actividades.get(i).get("doc_nombre"));
					Log.i("info", "" + Actividades.get(i).get("doc_url"));
				}
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

			progressDialog.dismiss();
			if (Actividades.size() == 0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						Documentos.this);
				builder.setMessage("No hay documentos disponibles")
						.setCancelable(false)
						.setPositiveButton("Aceptar",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										finish();
									}
								});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			} else {

			}

			lista = (ListView) findViewById(R.id.list);

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(
					getApplicationContext(),
					android.R.layout.simple_list_item_1, arrayDoc);

			lista.setAdapter(adapter);

			lista.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					Intent detalledoc = new Intent(Documentos.this,
							DocumentosDetalle.class);
					detalledoc.putExtra("url", getString(R.string.QRY_URL)
							+ arrayDocUrl[arg2]);
					startActivity(detalledoc);

					// mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+
					// webUrl);

				}
			});

		}

	}
}
